﻿/*
 * GdoSqlConnection
 * 
 * initial implementation : 20.9.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Petra.Gis.DataServices;

namespace Petra.Gis.DataServices.GDO.Sqlserver
{
  /// <summary>
  /// represents connection to mssql server, sqlConnection is sealed so this class inherits from
  /// DbConnection and wraps up normal sqlConnection
  /// thus using any property or function calls inside sqlConnection original
  /// </summary>
  public partial class GdoSqlConnection : SpatialDbConnection
  {

    //wrapped original connection to sql server ;-)
    private SqlConnection _sqlConnection;


    public GdoSqlConnection()
    {
      _sqlConnection = new SqlConnection();
      this.Extensions.Add("SqlConnection", _sqlConnection);
    }




    public GdoSqlConnection(string connectionString)
      :this()
    {
      _sqlConnection.ConnectionString = connectionString;
    }




    public override void Open()
    {
      _sqlConnection.Open();
    }





    public override void Close()
    {
      _sqlConnection.Close();
    }





    protected override DbCommand CreateDbCommand()
    {
      return this.CreateCommand();
    }





    new public GdoSqlCommand CreateCommand()
    { 
      GdoSqlCommand result = new GdoSqlCommand();
      result.Connection = this;
      return result;
    }





    public override string ConnectionString
    {
      get
      {
        return _sqlConnection.ConnectionString;
      }
      set
      {
        //recoverConnectionState(_sqlConnection.State, value);
        _sqlConnection.ConnectionString = value;
      }
    }





    public override string DataSource
    {
      get { return _sqlConnection.DataSource; }
    }





    public override string Database
    {
      get { return _sqlConnection.Database; }
    }





    public override void ChangeDatabase(string databaseName)
    {
      _sqlConnection.ChangeDatabase(databaseName);
    }





    protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
    {
      return this.BeginTransaction();
    }





    new public System.Data.SqlClient.SqlTransaction BeginTransaction()
    {
      return _sqlConnection.BeginTransaction();
    }





    public override string ServerVersion
    {
      get { return _sqlConnection.ServerVersion; }
    }





    public override ConnectionState State
    {
      get { return _sqlConnection.State; }
    }




    //public override ConnectionState XState 
    //{
    //  get { return this.State;}
    //  set 
    //  {
    //    recoverConnectionState(value, _sqlConnection.ConnectionString);
    //  }
    //}





    //protected void recoverConnectionState(ConnectionState savedState, string connectionStr)
    //{
    //  if (savedState == ConnectionState.Open && connectionStr != "")
    //    _sqlConnection.Open();
    //}




  }
}
